//
//  AppSetting.m
//  OrderSeat
//
//  Created by haiming on 13-10-4.
//  Copyright (c) 2013年 wangwei. All rights reserved.
//

#import "AppSetting.h"
#import "sys/utsname.h"

@implementation AppSetting

#pragma 设置是否第一次启动
+(void)setFirstLaunching:(BOOL)isFirst
{
    [AppSetting setObject:[NSNumber numberWithBool:isFirst] forKey:@"isFirstLaunching"];
}
#pragma 获取是否第一次启动
+(BOOL)isFirstLaunching
{
    return [[AppSetting objectForKey:@"isFirstLaunching"] boolValue];
}

#pragma 设置用户名信息
+ (void)setDataVersion:(int)version
{
    [AppSetting setObject:INT_TO_STR(version) forKey:@"dataVersion"];
}
#pragma 获取用户名信息
+ (int)dataVersion
{
    NSString *version = [AppSetting objectForKey:@"dataVersion"];
    if (version) {
        return [version intValue];
    }
    
    return 0;
}

+ (void)setIsOpenNotification:(BOOL)isOpen
{
    [AppSetting setObject:isOpen?@"1":@"0" forKey:@"isOpenNotification"];
}

+ (BOOL)isOpenNotification
{
    return [[AppSetting objectForKey:@"isOpenNotification"] intValue] == 1;
}

+ (void)setIsUpdatePhoto:(BOOL)isUpdate
{
    [AppSetting setObject:isUpdate?@"1":@"0" forKey:@"isUpdatePhoto"];
}

+ (BOOL)isUpdatePhoto
{
    return [[AppSetting objectForKey:@"isUpdatePhoto"] intValue] == 1;
}

#pragma 设置用户名信息
+ (void)setUserName:(NSString *)username
{
    [AppSetting setObject:username forKey:@"username"];
}
#pragma 获取用户名信息
+ (NSString *)userName
{
    return [AppSetting objectForKey:@"username"];
}

#pragma 设置用户手机号码信息
+ (void)setTel:(NSString *)tel
{
    [AppSetting setObject:tel forKey:@"tel"];
}
#pragma 获取用户手机号码信息
+ (NSString *)tel
{
    return [AppSetting objectForKey:@"tel"];
}

#pragma 设置是否登录
+ (void)setIsLogin:(BOOL)isLogin
{
    [AppSetting setObject:[NSNumber numberWithBool:isLogin] forKey:@"isLogin"];
}

#pragma 获取是否登录
+ (BOOL)isLogin
{
    BOOL isLogin = [[AppSetting objectForKey:@"isLogin"] boolValue];
    return isLogin;
}

#pragma 设置用户头像
+ (void)setUserPhoto:(NSString *)photo
{
    [AppSetting setObject:photo forKey:@"touxiang"];
}
#pragma 获取用户头像信息
+ (NSString *)userPhoto
{
    return [AppSetting objectForKey:@"touxiang"];
}

+ (void)setNickName:(NSString *)nickname
{
    [AppSetting setObject:nickname forKey:@"nicheng"];
}

+ (NSString *)nickName
{
    return [AppSetting objectForKey:@"nickname"];
}

+ (void)loginOut
{
    [AppSetting setUserPhoto:S_EMPTY];
    [AppSetting setUserName:S_EMPTY];
    [AppSetting setTel:S_EMPTY];
    [AppSetting setIsLogin:NO];
}

+ (void)setLocation:(CLLocation *)location
{
    NSString *latitude = [NSString stringWithFormat:@"%g",location.coordinate.latitude];
    NSString *longitude = [NSString stringWithFormat:@"%g",location.coordinate.longitude];
    NSLog(@"latitude:%@",latitude);
    NSLog(@"longitude:%@",longitude);
    [AppSetting setObject:latitude forKey:@"latitude"];
    [AppSetting setObject:longitude forKey:@"longitude"];
}

+ (NSString *)latitude
{
//    return @"36.065586";
    return [AppSetting objectForKey:@"latitude"];
}

+ (NSString *)longitude
{
//    return @"103.824342";
    return [AppSetting objectForKey:@"longitude"];
}

+ (void)setClearLikeDate
{
    NSDate *date = [NSDate date];
    NSString *dateStr = [date toStringWithFormat:@"yyyyMMdd"];
    [AppSetting setObject:dateStr forKey:@"clearDate"];
}

+ (NSString *)clearLikeDate
{
    return [AppSetting objectForKey:@"clearDate"];
}

+ (void)setCityId:(NSString *)cityId
{
    [AppSetting setObject:cityId forKey:@"cityId"];
}

+ (NSString *)cityId
{
    return [AppSetting objectForKey:@"cityId"];
}

+ (void)setCityName:(NSString *)cityName
{
    [AppSetting setObject:cityName forKey:@"cityName"];
}

+ (NSString *)cityName
{
    NSString *cityName = [AppSetting objectForKey:@"cityName"];
    if (!cityName || [cityName isEqualToString:@""]) cityName = @"兰州";
    return cityName;
}

#pragma 简化获取值
+ (id)objectForKey:(NSString *)key
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    id value = [defaults objectForKey:key];
    if (value == nil || value == NULL) {
        return @"";
    }
    return value;
}
#pragma 简化设置值
+ (void)setObject:(id)object forKey:(NSString*)key
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:object forKey:key];
    [defaults synchronize];
}

+ (NSString *)documentDirectory
{
    NSArray* paths =
    NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSString* documentsDirectory = [paths objectAtIndex:0];
    
    return documentsDirectory;
}

+ (NSString *)filePath:(NSString*)filename
{
    NSString* documentsDirectory = [AppSetting documentDirectory];
    NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:filename];
    
    return fullPathToFile;
}

+ (NSString*)deviceString
{
    // 需要#import "sys/utsname.h"
    struct utsname systemInfo;
    uname(&systemInfo);
    NSString *deviceString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
    
    if ([deviceString isEqualToString:@"iPhone1,1"])    return @"iPhone 1G";
    if ([deviceString isEqualToString:@"iPhone1,2"])    return @"iPhone 3G";
    if ([deviceString isEqualToString:@"iPhone2,1"])    return @"iPhone 3GS";
    if ([deviceString isEqualToString:@"iPhone3,1"])    return @"iPhone 4";
    if ([deviceString isEqualToString:@"iPhone4,1"])    return @"iPhone 4S";
    if ([deviceString isEqualToString:@"iPhone5,2"])    return @"iPhone 5";
    if ([deviceString isEqualToString:@"iPhone3,2"])    return @"Verizon iPhone 4";
    if ([deviceString isEqualToString:@"iPod1,1"])      return @"iPod Touch 1G";
    if ([deviceString isEqualToString:@"iPod2,1"])      return @"iPod Touch 2G";
    if ([deviceString isEqualToString:@"iPod3,1"])      return @"iPod Touch 3G";
    if ([deviceString isEqualToString:@"iPod4,1"])      return @"iPod Touch 4G";
    if ([deviceString isEqualToString:@"iPad1,1"])      return @"iPad";
    if ([deviceString isEqualToString:@"iPad2,1"])      return @"iPad 2 (WiFi)";
    if ([deviceString isEqualToString:@"iPad2,2"])      return @"iPad 2 (GSM)";
    if ([deviceString isEqualToString:@"iPad2,3"])      return @"iPad 2 (CDMA)";
    if ([deviceString isEqualToString:@"i386"])         return @"Simulator";
    if ([deviceString isEqualToString:@"x86_64"])       return @"Simulator";
    NSLog(@"NOTE: Unknown device type: %@", deviceString);
    return deviceString;
}

@end
